#Author: 0x9DEFA478

[CmdletBinding(PositionalBinding=$false)]
Param(
  $BUILD_PREFIX="",
  [Parameter(Mandatory)]
  $InputExec,
  [Parameter(Mandatory,ValueFromRemainingArguments=$true)]
  [string[]]$LibRelDirList
)


$FullName=Get-Command "${BUILD_PREFIX}gcc" | Select-Object -ExpandProperty Definition


$ExecFullPath=(Get-Item $InputExec).DirectoryName

#  -ErrorAction SilentlyContinue -ErrorVariable error
&"${BUILD_PREFIX}objdump.exe" -x $InputExec | Where-Object { $_ -match "DLL Name:" } | ForEach-Object {
  
  $item=${_}.Trim() -replace "DLL Name:"
  $item=$item.Trim()
  Write-Host -ForegroundColor:Blue ${item} -NoNewline

  $ResDir=$null
  foreach($LibRelDir in $LibRelDirList){
    $LibPath=(Get-Item $FullName).Directory.FullName+$LibRelDir

    if(Test-Path "${LibPath}/${item}" -PathType Leaf){
      Copy-Item -Recurse "${LibPath}/${item}" "${ExecFullPath}/${item}"
      $ResDir=$LibRelDir
      break
    }
    
  }
  if($ResDir){
    Write-Host -ForegroundColor:Green " OK"  -NoNewline
    Write-Host " in ${ResDir}"
  }else{
    Write-Host -ForegroundColor:Red " FAIL"
  }
}

